(function (factory) {
    const nameSpace = 'field_6255379057620178484';
    if (!window[nameSpace]) {
        const Builder = factory();
        window[nameSpace] = {
            instance: {}
        };
        window[nameSpace].init = function (options) {
            window[nameSpace].instance[options.privateId] = new Builder(options);
        };
        window[nameSpace].isNotNull = function (obj) {
            return true;
        };
    }
})
(function () {
    /**
     * 构造函数
     * @param options
     * @constructor
     */
    function App(options) {
        const self = this;
        //初始化参数
        self.initParams(options);
        //初始化dom
        self.initDom();
        //事件
        self.events();
    }

    App.prototype = {
        initParams: function (options) {
            const self = this;
            self.adaptation = options.adaptation;
            self.privateId = options.privateId;
            self.messageObj = options.getData;
            self.preUrl = options.url_prefix;
        },
        initDom: function () {
            const self = this;
            dynamicLoading.css(self.preUrl + 'css/formQueryBtn.css');
            self.appendChildDom();
        },
        events: function () {
            const self = this;
            // 监听是否数据刷新
            self.adaptation.ObserverEvent.listen('Event' + self.privateId, function () {
                self.messageObj = self.adaptation.childrenGetData(self.privateId);
                self.appendChildDom();
            });
        },
        appendChildDom: function () {
            const self = this;
            const content = self.messageObj.formdata.content;
            //当点击电子签章时调用此方法
            const doSignature = function () {
                const process = top.$.progressBar({
                    text: "开始进行电子签，请等待..."
                });
                csdk.core.preSave(function (err, data) {
                    var params = new Object();
                    //表单ID
                    params.form = content.contentTemplateId;
                    //表单数据ID
                    params.formData = content.contentDataId;
                    if (err) {//调用失败
                        process.close();
                        $.alert("提交数据报错");
                        return;
                    } else {//调用成功
                        $.ajax({
                            type: 'post',
                            async: true,
                            url: (_ctxPath ? _ctxPath : '/seeyon') + "/rest/kkESign/doSignature",
                            dataType: 'json',
                            data: JSON.stringify(params),
                            contentType: 'application/json',
                            success: function (res) {//签章成功
                                process.close();
                                if (res.code != 0) {
                                    $.alert(res.message);
                                } else {
                                    //合同Id

                                    window.open(res.data.data.url);
                                    $.alert("电子签发起成功，请在跳出的界面完成用印后F5刷新此页面，获取到用印后的附件之后 再点击提交！");

                                }
                            },
                            error: function (e) {//签章失败
                                process.close();
                                $.alert("进行电子签异常.");
                            }
                        });
                    }
                });
            };

            const domStructure = '<section class="customButton_box_content">' +
                '<div class="customButton_class_box ' + self.privateId + '" title="' + self.messageObj.display.escapeHTML() + '">' + self.messageObj.display.escapeHTML() + '</div>' +
                '</section>';
            //渲染隐藏权限
            if (self.messageObj.auth == 'edit') {
                document.querySelector('#' + self.privateId).innerHTML = domStructure;
                document.querySelector('.' + self.privateId).removeEventListener('click', doSignature);
                document.querySelector('.' + self.privateId).addEventListener('click', doSignature);
            } else {
                document.querySelector('#' + self.privateId).innerHTML = '<div class="cap4-text__browse" style="line-height: 1.8; color: rgb(0, 0, 0) !important;"></div>';
            }
        }
    };

    var dynamicLoading = {
        css: function (path) {
            if (!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            const head = document.getElementsByTagName('head')[0];
            const link = document.createElement('link');
            link.href = path;
            link.rel = 'stylesheet';
            link.type = 'text/css';
            head.appendChild(link);
        },
        js: function (path) {
            if (!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            const head = document.getElementsByTagName('head')[0];
            const script = document.createElement('script');
            script.src = path;
            script.type = 'text/javascript';
            head.appendChild(script);
        }
    }
    return App;
});